﻿@model WayCloud.ViewModel.User.UserCondition
@{
    ViewBag.Title = "用户管理";
}
<script type="text/javascript">

    function createEdit(cellvalue, options, rowData) {
        return "<a href='##' name='editLink' rowIndex='" + options.rowId + "' style='color:#0045FF'>修改</a>";
    }

    function createResetPassword(cellvalue, options, rowData) {
        return "<a href='##' name='resetPasswordLink' rowIndex='" + options.rowId + "' style='color:#0045FF'>重置密码</a>";
    }

    function getRowData(rowIndex) {
        return $("#list").getRowData(rowIndex);
    }

    function serializeCondition() {
        return $("#divSearch").children().serialize();
    }

    function createModelDialog(title, width, height) {
        $("#editDiv").dialog({
            modal: true,
            width: width,
            title: title,
            draggable: false,
            resizable: false,
            open: function (event) {
                $("#editDiv").data("dialog", event.target);
            },
            beforeClose: function () {
                $("#list").trigger("reloadGrid");
                $("#editDiv").dialog("destroy");
            }
        });
    }

    function gridComplete() {
        $("a[name='editLink']").click(function () {
            var rowData = getRowData($(this).attr("rowIndex"));
            var id = rowData.Id;
            var title = "修改用户信息：" + rowData.UserName;
            $("#editDiv").load("/user/find/" + id, function (result) {
                createModelDialog(title, 600);
            });
        });

        $("a[name='resetPasswordLink']").click(function () {
            var rowData = getRowData($(this).attr("rowIndex"));
            if (confirm('确定要重置用户:"' + rowData.UserName + '"的密码？')) {
                var id = rowData.Id;
                $.get('/user/ResetPassword/' + id, function (result) {
                    if (result == "error") {
                        alert("出现内部错误，请重试！");
                    }
                    else {
                        alert("用户密码已经被重置为：" + result);
                    }
                });
            }



        });
    }

    $().ready(function () {
        $.ajaxSetup({ cache: false });
        $("#list").jqGrid({
            url: '/user/list/',
            datatype: "json",
            colNames: ['编号', '帐号', '姓名', '角色', '电子邮箱', '电话', '修改', '重置密码'],
            colModel: [
                { name: 'Id', index: 'Id', width: 200, align: 'center', hidden: true },
                { name: 'AccountName', index: 'AccountName', width: 150, align: 'left' },
                { name: 'UserName', index: 'UserName', width: 70, align: 'left' },
                { name: 'Role', index: 'Role', width: 70, align: 'left' },
                { name: 'Email', index: 'Email', width: 100, align: "left" },
                { name: 'Phone', index: 'Phone', width: 100, align: 'right' },
                { name: 'Modify', index: 'Id', width: 80, align: "center", sortable: false, formatter: createEdit },
                { name: 'ResetPassword', index: 'Id', width: 100, align: "center", sortable: false, formatter: createResetPassword }
            ],
            autowidth: true,
            hidegrid: false,
            height: "auto",
            rowNum: 10,
            pager: '#pager',
            emptyrecords: "没有满足条件的记录",
            viewrecords: true,
            caption: "用户列表",
            multiselect: false,
            jsonReader: {
                repeatitems: false
            },
            gridComplete: gridComplete
        }).navGrid('#pager', { edit: false, add: false, del: false, view: false, search: false });

        $("#btnSearch").button().click(function () {
            var url = '/user/list/?' + serializeCondition();
            $("#list").jqGrid('setGridParam', { url: url }).trigger("reloadGrid");
        });

    });

</script>
<div>
    <div style="margin: 5px; text-align: left;" id="divSearch">
        @Html.LabelFor(p => p.AccountName):@Html.EditorFor(p => p.AccountName)
        @Html.LabelFor(p => p.UserName):@Html.EditorFor(p => p.UserName)
        <input type="button" id="btnSearch" value="查  询" />
    </div>
    <table id="list" width="100%">
    </table>
    <div id="pager">
    </div>
</div>
<div id="editDiv">
</div>
